import { create } from 'zustand';
import { Diary, ChatMessage, User, Stats } from '../types';

// 应用状态接口
interface AppState {
  // 用户相关
  user: User | null;
  isLoggedIn: boolean;
  
  // 日记相关
  diaries: Diary[];
  currentDiary: Diary | null;
  isLoadingDiaries: boolean;
  
  // AI对话相关
  chatMessages: ChatMessage[];
  isLoadingChat: boolean;
  
  // 统计数据
  stats: Stats | null;
  
  // 应用状态
  isLoading: boolean;
  error: string | null;
  
  // Actions
  setUser: (user: User | null) => void;
  setDiaries: (diaries: Diary[]) => void;
  addDiary: (diary: Diary) => void;
  updateDiary: (diary: Diary) => void;
  deleteDiary: (id: number) => void;
  setCurrentDiary: (diary: Diary | null) => void;
  
  setChatMessages: (messages: ChatMessage[]) => void;
  addChatMessage: (message: ChatMessage) => void;
  
  setStats: (stats: Stats) => void;
  setLoading: (loading: boolean) => void;
  setError: (error: string | null) => void;
  
  // 复合操作
  logout: () => void;
  clearData: () => void;
}

// 创建状态管理store
export const useAppStore = create<AppState>((set, get) => ({
  // 初始状态
  user: null,
  isLoggedIn: false,
  diaries: [],
  currentDiary: null,
  isLoadingDiaries: false,
  chatMessages: [],
  isLoadingChat: false,
  stats: null,
  isLoading: false,
  error: null,
  
  // Actions
  setUser: (user) => set({ user, isLoggedIn: !!user }),
  
  setDiaries: (diaries) => set({ diaries, isLoadingDiaries: false }),
  
  addDiary: (diary) => set((state) => ({
    diaries: [diary, ...state.diaries]
  })),
  
  updateDiary: (diary) => set((state) => ({
    diaries: state.diaries.map(d => d.id === diary.id ? diary : d),
    currentDiary: state.currentDiary?.id === diary.id ? diary : state.currentDiary
  })),
  
  deleteDiary: (id) => set((state) => ({
    diaries: state.diaries.filter(d => d.id !== id),
    currentDiary: state.currentDiary?.id === id ? null : state.currentDiary
  })),
  
  setCurrentDiary: (diary) => set({ currentDiary: diary }),
  
  setChatMessages: (chatMessages) => set({ chatMessages, isLoadingChat: false }),
  
  addChatMessage: (message) => set((state) => ({
    chatMessages: [...state.chatMessages, message]
  })),
  
  setStats: (stats) => set({ stats }),
  setLoading: (isLoading) => set({ isLoading }),
  setError: (error) => set({ error }),
  
  // 复合操作
  logout: () => set({
    user: null,
    isLoggedIn: false,
    diaries: [],
    currentDiary: null,
    chatMessages: [],
    stats: null,
  }),
  
  clearData: () => set({
    diaries: [],
    currentDiary: null,
    chatMessages: [],
    stats: null,
    error: null,
  }),
}));

// 选择器函数
export const selectUser = (state: AppState) => state.user;
export const selectDiaries = (state: AppState) => state.diaries;
export const selectCurrentDiary = (state: AppState) => state.currentDiary;
export const selectChatMessages = (state: AppState) => state.chatMessages;
export const selectStats = (state: AppState) => state.stats;
export const selectIsLoading = (state: AppState) => state.isLoading;
export const selectError = (state: AppState) => state.error; 